package cn.ygh.bbo.framerwork.config;

import cn.ygh.bbo.framerwork.config.properties.DynamicProperties;
import cn.ygh.bbo.framerwork.config.properties.ShiroProperties;
import cn.ygh.bbo.framerwork.web.SpringContext;
import cn.ygh.bbo.framerwork.web.TokenService;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @author ygh
 * @Desc 全局通用配置
 * @date 2019/11/21
 */
@Configuration
public class GlobalAutoConfiguration {

    /**
     * 通用属性配置
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "dynamic")
    public DynamicProperties dynamicProperties(){
        return new DynamicProperties();
    }

    /**
     * shiro通用属性配置
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "shiro")
    public ShiroProperties shiroProperties(){
        return new ShiroProperties();
    }

    /**
     * 通用spring环境类
     * @return
     */
    @Bean
    public SpringContext springContext(){
        return new SpringContext();
    }

    /**
     * 通用验证码生成器
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public Producer producer(){
        DefaultKaptcha defaultKaptcha=new DefaultKaptcha();
        Properties properties=new Properties();
        properties.setProperty("kaptcha.border", "yes");
        properties.setProperty("kaptcha.border.color", "105,179,90");
        properties.setProperty("kaptcha.textproducer.font.color", "blue");
        properties.setProperty("kaptcha.image.width", "125");
        properties.setProperty("kaptcha.image.height", "45");
        properties.setProperty("kaptcha.session.key", "code");
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        Config config=new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }

    /**
     * 通用token令牌生成器
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public TokenService tokenService(){
        return new TokenService();
    }


}
